package com.whut.mobilesafe.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

/**
 * 作    者:yubo.xiaoyubo
 * 时    间：2015/12/24 10:28
 * 版    本：1.0
 * 描    述：
 */
public abstract class BaseStepActivity extends Activity {

    private GestureDetector mGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if (Math.abs(e1.getRawY() - e2.getRawY()) > 100) {
                    Toast.makeText(BaseStepActivity.this, "竖直滑动的距离太大了啦", Toast.LENGTH_SHORT).show();
                    return true;
                } else if (Math.abs(velocityX) < 100) {
                    Toast.makeText(BaseStepActivity.this, "滑动速度太慢", Toast.LENGTH_SHORT).show();
                    return true;
                } else if (e2.getRawX() - e1.getRawX() > 200) {
                    //向右滑
                    showPrevious();
                    return true;
                } else if (e1.getRawX() - e2.getRawX() > 200) {
                    //向左滑
                    showNext();
                    return true;
                }
                return super.onFling(e1, e2, velocityX, velocityY);
            }
        });

    }

    //抽象方法,让子类activity进行实现
    public abstract void showNext();

    public abstract void showPrevious();

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mGestureDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

    public void next(View view){
        showNext();
    }

    public void previous(View view){
        showPrevious();
    }
}
